//============================================================================
// Name        : Server.cpp
// Author      : ma
// Version     :
// Copyright   : Your copyright notice
// Description : Server Class
//============================================================================

//from: http://tldp.org/LDP/LG/issue74/tougher.html
#include "ServerSocket.h"
#include "SocketException.h"
#include <string>

#include "ServerThread.h"

#include <iostream>

int main(int argc, int argv[]) {

	std::cout << "running....\n";

	try {
		// Create the socket
		ServerSocket server(3000);

		while (true) {

			// Allocate a new ServerSocket for each Client
			ServerSocket *new_sock = new ServerSocket();

			// Wait for a client to connect
			server.accept(*new_sock);

			ServerT * sThread = new ServerT(Thread::PRIORITY_NORMAL, "ServerThread", new_sock);
			sThread->start();
			std::cout << "Thread started....\n";
			sleep(1);
		}
	} catch (SocketException& e) {
		std::cout << "Exception was caught:" << e.description()
				<< "\nExiting.\n";
	}
	return 0;
}
